<?php

namespace Biz;

use Base\BaseOrmModel;
use Base\Exception\NoRecordException;
class UserRank extends \Base\BaseBiz{

    /**
     * @param $where
     * @param $offset
     * @param $size
     * @return mixed
     */
    public function getUserRankList($where, $offset, $size)
    {
        $rank_orm = new \Orm\UserRank\UserRankModel();
        return $rank_orm->getRankStorageList($where, $offset, $size);
    }

    /**
     * @param $data
     * @return bool
     * 积分用户派奖
     */
    public function insert($data){
        $user_rank = new \Orm\UserRank\UserRankClass();
        $user_rank['user_id'] = $data['user_id'];
        $user_rank['nickname'] = $data['nickname'];
        $user_rank['product_id'] = $data['product_id'];
        $user_rank['user_sort'] = $data['user_sort'];
        $user_rank['user_score'] = $data['user_score'];
        $user_rank['type'] = $data['type'];
        $user_rank['week_type'] = $data['week_type'];
        $user_rank['read'] = 0;
        $user_rank->insert();
        if($user_rank['id']){
            return true;
        }else{
            return false;
        }
    }

    public function update($data){
        $user_rank = new \Orm\UserRank\UserRankClass();
        $user_rank->loadById($data['id']);
        if(isset($data['logistics_num'])){
            $user_rank['logistics_num'] = $data['logistics_num'];
        }
        if(isset($data['status'])){
            $user_rank['status'] = $data['status'];
        }
        if(isset($data['read'])){
            $user_rank['read'] = $data['read'];
        }
       return $user_rank->update();
    }

    //更新实物奖品状态为已阅读
    public function updateByUid($user_id){
        $user_rank = new \Orm\UserRank\UserRankModel();
        return $user_rank->updateByUid($user_id);
    }

    /**
     * @param $uid
     * @param $type
     * @param string $week_type
     * @return bool
     */
    public function getUserRankByWhere($uid,$type,$week_type =''){
        $user_rank = new \Orm\UserRank\UserRankModel();
        $where = " `user_id` = {$uid}  AND `type` = {$type} ";
        if($type == 1){//周排行
            $where .= " AND `week_type` = {$week_type}";
        }
        $re = $user_rank->getUserRankByWhere($where);
        if(!empty($re)){
            return true;
        }else{
            return  false;
        }
    }

    public function getById($id){
        $userRank = new \Orm\UserRank\UserRankClass();
        return $userRank->loadById($id);
    }
}